package com.gongj.main;

import com.gongj.bean.User;
import org.springframework.beans.factory.support.AbstractBeanDefinitionReader;
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
import org.springframework.beans.factory.xml.XmlBeanDefinitionReader;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.ClassPathResource;

public class Main {
	public static void main1(String[] args) {
		ClassPathResource resource = new ClassPathResource("spring-config.xml");
		DefaultListableBeanFactory factory = new XmlBeanFactory(resource);
		User user = (User)factory.getBean("user");
		System.out.println(user);
	}

	public static void main(String[] args) {

		//设置加载环境
		//System.setProperty("spring.profiles.active", "dev");

		//ClassPathResource resource = new ClassPathResource("spring-config.xml");
		DefaultListableBeanFactory factory = new DefaultListableBeanFactory();
		AbstractBeanDefinitionReader reader = new XmlBeanDefinitionReader(factory);
		int i = reader.loadBeanDefinitions("spring-config.xml");
		System.out.println("本次加载的Bean数量：" + i);
		// 根据BeanName获取Bean
		User user = (User)factory.getBean("user");
		System.out.println("user" + user);
		User.Vip vip = user.getVip();
		System.out.println("vip：" + vip.getVipLevel());

		//根据别名获取Bean
		User user2 = (User)factory.getBean("user2");
		System.out.println("user2：" + user2);
		User user4 = (User)factory.getBean("user4");
		System.out.println("user4：" + user4);
		//根据bean名称获取别名数组
		String[] users = factory.getAliases("user");
		for (String s : users) {
			System.out.println(s);
		}
	}
}
